<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Safty-Page for delete categorys
 * Prints a list of the categorys and testing
 * if subcategorys or articles exist...
 * get category.delete.tpl.php
 * used from category.page.php
 * 
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
$messagetext = 0;

// Check for Sub-Categorys
foreach ($_POST['marker'] as $idx)
{
	$idx = $refValidate->cleanslash($idx);
	if ($result = db::getInstance()->rawQuery("SELECT category_id, category_name FROM category " .
		"WHERE category_top = '" . $idx . "'"))
	{
		$messagetext = 1;
		message::setErrorRow('<h2>' . _('The category(s) still have subcategorys:') . '</h2>');

		db::getInstance()->where("category_id", $idx);
		$resultCategory = db::getInstance()->getOne("category");

		message::setErrorRow($idx . ' - ' . $resultCategory['category_name']);

		foreach ($result as $value)
		{
			message::setErrorRow('&nbsp&nbsp&nbsp&nbsp&nbsp' . $value['category_id'] . ' - ' .
				$value['category_name']);
		}
	}
}

// Check for Category-Article
foreach ($_POST['marker'] as $idx)
{
	$idx = $refValidate->cleanslash($idx);
	if ($result = db::getInstance()->rawQuery("SELECT product_id, product_name FROM product " .
		"WHERE product_category = '" . $idx . "'"))
	{
		$messagetext = 1;
		message::setErrorRow('<h2>' . _('The category still have articles:') . '</h2>');

		foreach ($result as $value)
		{
			message::setErrorRow($value['product_id'] . ' - ' . $value['product_name']);
		}
	}
}

if ($messagetext === 1)
{ // An error occurs...
	message::setErrorRow('<div class="delete_error">
					 <form action="index2.php?page = article">
					 <button type="button" onclick="window.location.href = \'index2.php?page=article\';">
					 <b>' . _('Cancel') . '</b>
					 </button>
					 </form>
					 </div>');
	$refPage->addtpl('content', '/empty.tpl.php');
}
else
{ // "delete" Categorys
	$data = array();
	$deleteIds = '';
	$refPage->addtpl('content', '/article/category.delete.tpl.php');
	$refPage->assign('page_title', _('Can the following category(s) be deleted?'));
	$refPage->assign('summary', _('Entries are moved into the basket'));

	$refPage->assign('category_id', _('ID'));
	$refPage->assign('category_name', _('Category-Name'));

	foreach ($_POST['marker'] as $idx)
	{
		$idx = $refValidate->cleanslash($idx);
		//$result = db::getInstance()->get_row("SELECT category_id, category_name FROM category " .
		//	"WHERE category_id = '" . $idx . "'");
		db::getInstance()->where("category_id", $idx);
		$result = db::getInstance()->getOne("category");

		$data[] = array('category_idx' => $result['category_id'],
		 'category_namex' => $result['category_name']);
		$deleteIds .= $result['category_id'] . '-';
	}

	$refPage->assign('form',
		'action="index2.php?page=article&sub=category&func=deleteNow" method="post"');
	$refPage->assign('data', $data);
	$refPage->assign('button',
		'<input type="hidden" name="deleteId" value="' . $deleteIds . '" />' .
		'<input type="submit" name="submit" id="delete" value="' .
		_('Delete Category(s)') . '" />' .
		'<button type="button" value="' . _('Cancel') . '"' .
		'onClick="parent.location=\'index2.php?page=article&sub=category\'">' .
		_('Cancel') . '</button>');

	$refPage->assign('buttonDeleteLink', 'index2.php?page=article&sub=category&func=delete');
}
?>